package com.example.publishDemo.Service.Impl;

import com.example.publishDemo.Mapper.ArticleMapper;
import com.example.publishDemo.Mapper.CommentMapper;
import com.example.publishDemo.Mapper.UserMapper;
import com.example.publishDemo.Model.Article;
import com.example.publishDemo.Model.ArticleReport;
import com.example.publishDemo.Model.ArticleScore;
import com.example.publishDemo.Model.ArticleStar;
import com.example.publishDemo.Service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Resource
    ArticleMapper articleMapper;

    @Resource
    CommentMapper commentMapper;

    @Resource
    UserMapper userMapper;

    @Override
    public int save(Article article) {
        return articleMapper.save(article);
    }

//    @Override
//    public List<Article> rangeForTime2(int start, int end) {
//        return articleMapper.rangeForTime2(start, end);
//    }

    @Override
    public List<Article> forCommend() {
        return articleMapper.forCommend();
    }

    @Override
    public int increaseView(int flag) {
        return articleMapper.increaseView(flag);
    }

    @Override
    public int averageScore(double flag, int a_id) {
        return articleMapper.averageScore(flag, a_id);
    }

    @Override
    public int saveScore(int u_id, int a_id, double flag) {
        return articleMapper.saveScore(u_id, a_id, flag);
    }

    @Override
    public List<ArticleScore> isScore(int u_id, int a_id) {
        return articleMapper.isScore(u_id, a_id);
    }

    @Override
    public Article getOne(int a_id) {
        return articleMapper.getOne(a_id);
    }

    @Override
    public int star(int u_id, int a_id) {
        return articleMapper.star(u_id, a_id);
    }

    @Override
    public int delStar(int u_id, int a_id) {
        return articleMapper.delStar(u_id, a_id);
    }

    @Override
    public boolean isStar(int u_id, int a_id) {
        int star = articleMapper.isStar(u_id, a_id).size();
        if (star > 0) {
            return true;
        }
        return false;
    }

    @Override
    public int staredNum(int a_id) {
        return articleMapper.staredNum(a_id);
    }

    @Override
    public int report(int a_id, int u_id) {
        return articleMapper.report(a_id, u_id);
    }

    @Override
    public ArticleReport isReport(int a_id, int u_id) {
        return articleMapper.isReport(a_id, u_id);
    }

    @Override
    public List<Article> articleRange(int start, int end, String activeName, String keyword,int u_id) {
        switch (activeName) {
            case "first":
                return articleMapper.rangeForTime(start,end,keyword);
            case "second":
                List<Integer> orderByRid= commentMapper.groupArticleByRid();  //返回评论数量从高到底的文章id(由于建表时命名错误，和其它表的a_id命名不同了……)
//                List<Integer> strings = orderByRid.subList(start, end);
                List<Article> list = new ArrayList<Article>();
                for (int i = 0; i < orderByRid.size(); i++) {
                    Article article = articleMapper.findByAidAndKeyword(orderByRid.get(i),keyword);
                    if(article != null) list.add(article);
                }

                return subList(list,start,end);
            case "third":
                List<Article> list1 = articleMapper.orderByViewsAndKeyword(start, end, keyword);
                return list1;
            case "fourth":
                List<Article> list2 = articleMapper.orderByScoreAndKeyword(start, end, keyword);
                return list2;
            case "fifth":
                List<Integer> userFocused = userMapper.userFocused(u_id);
                List<Article> list3 = new ArrayList<Article>();
                for (int i = 0; i < userFocused.size(); i++) {
                    List<Article> byAuthorIdAndKeyword = articleMapper.findByAuthorIdAndKeyword(userFocused.get(i), keyword);
                    if(byAuthorIdAndKeyword != null) list3.addAll(byAuthorIdAndKeyword);
                }
                return subList(list3,start,end);
            default:
                break;
        }
        return null;
    }

    List<Article> subList(List list,int start,int end) {
        if (list.size() > start) {
            if (list.size() > end) {
                return list.subList(start, end);
            }else {
                return list.subList(start, list.size());
            }
        }
        return null;
    }

    @Override
    public int articleRangeNum(String activeName, String keyword,int u_id) {
        switch (activeName) {
            case "first":
                return articleMapper.rangeForTimeNum(keyword);
            case "second":
                List<Integer> orderByRid= commentMapper.groupArticleByRid();  //返回评论数量从高到底的文章id(由于建表时命名错误，和其它表的a_id命名不同了……)
//                List<Integer> strings = orderByRid.subList(start, end);
                List<Article> list = new ArrayList<Article>();
                for (int i = 0; i < orderByRid.size(); i++) {
                    Article article = articleMapper.findByAidAndKeyword(orderByRid.get(i),keyword);
                    if(article != null) list.add(article);
                }
                return list.size();
            case "third":
                return articleMapper.orderByViewsAndKeywordNum(keyword);
            case "fourth":
                return articleMapper.orderByScoreAndKeywordNum(keyword);
            case "fifth":
                List<Integer> userFocused = userMapper.userFocused(u_id);
                List<Article> list3 = new ArrayList<Article>();
                for (int i = 0; i < userFocused.size(); i++) {
                    List<Article> byAuthorIdAndKeyword = articleMapper.findByAuthorIdAndKeyword(userFocused.get(i), keyword);
                    if(byAuthorIdAndKeyword != null) list3.addAll(byAuthorIdAndKeyword);
                }
                return list3.size();
            default:
                break;
        }
        return 0;
    }

    @Override
    public int allViewsForId(int u_id) {
        return articleMapper.allViewsForId(u_id);
    }

    @Override
    public int maxView(int u_id) {
        return articleMapper.maxView(u_id);
    }

    @Override
    public List<Article> getAllArticleForId(int u_id) {
        return articleMapper.getAllArticleForId(u_id);
    }

    @Override
    public int change(Article article) {
        return articleMapper.change(article);
    }

    @Override
    public List<Integer> GetAllCollectArticle(int u_id) {
        return articleMapper.GetAllCollectArticle(u_id);
    }

    @Override
    public List<Article> getArticleForTag(String tag) {
        return articleMapper.getArticleForTag(tag);
    }

    @Override
    public int articleNumByTag(String tag) {
        return articleMapper.articleNumByTag(tag);
    }

    @Override
    public int reReport(int r_id) {
        return articleMapper.reReport(r_id);
    }

    @Override
    public int countAllArticle() {
        return articleMapper.countAllArticle();
    }



}
